﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X22.DB.Extension.DBContext;
using X22.DB.Extension.Options;

namespace X22.DB.Extension.Extensions
{
    public static class X22DBExtension
    {
        public static IServiceCollection AddX22DB(this IServiceCollection services, DBType dbType)
        {
            return services;
        }



        public static IServiceCollection AddX22DBContext(this IServiceCollection services,Action<DBOption> option)
        {
            var dbOption = new DBOption();
            option.Invoke(dbOption);

            var str = "";
            services.AddDbContext<X22DBContext>(options => options.UseSqlServer(str));

            //var connectionString = Configuration.GetConnectionString("MySqlConnection");
            ServerVersion serverVersion = ServerVersion.AutoDetect(str);
            //var serverVersion = new MySqlServerVersion(new Version(8, 0, 27));
            services.AddDbContext<X22DBContext>(options => options.UseMySql(str, serverVersion));
            return services;
        }
        public static void ConfigureServices(IServiceCollection services)
        {
            // Replace with your connection string.
            var connectionString = "server=localhost;user=root;password=1234;database=ef";

            // Replace with your server version and type.
            // Use 'MariaDbServerVersion' for MariaDB.
            // Alternatively, use 'ServerVersion.AutoDetect(connectionString)'.
            // For common usages, see pull request #1233.
            var serverVersion = new MySqlServerVersion(new Version(8, 0, 27));

            // Replace 'YourDbContext' with the name of your own DbContext derived class.
            services.AddDbContext<YourDbContext>(
                dbContextOptions => dbContextOptions
                    .UseMySql(connectionString, serverVersion)
                    // The following three options help with debugging, but should
                    // be changed or removed for production.
                    .LogTo(Console.WriteLine, LogLevel.Information)
                    .EnableSensitiveDataLogging()
                    .EnableDetailedErrors()
            );
        }
    }
}
